package com.pleague.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static SimpleDateFormat YYYYMMDD = new SimpleDateFormat(
			"yyyy-MM-dd");
	
	private static SimpleDateFormat YYYYMMDD2 = new SimpleDateFormat(
			"yyyyMMdd");
	
	private static SimpleDateFormat YYYYMMDDHHMMSS = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	private static SimpleDateFormat YYYYMMDDHHMMSS2 = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	
	private static SimpleDateFormat HHMMSS = new SimpleDateFormat(
			"HH:mm:ss");
	
	public static String formatDateYYYYMMDD(Date date) {
		return YYYYMMDD.format(date);
	}
	
	public static String formatDateYYYYMMDD2(Date date) {
		return YYYYMMDD2.format(date);
	}
	
	public static String formatDateYYYYMMDDHHMMSS(Date date) {
		return YYYYMMDDHHMMSS.format(date);
	}
	
	public static String formatDateYYYYMMDDHHMMSS2(Date date) {
		return YYYYMMDDHHMMSS2.format(date);
	}
	
	public static String formatDateHHMMSS(Date date) {
		return HHMMSS.format(date);
	}
	
	public static String formatDate(Date date,String pattern ) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 日期按天计算(负数为减)
	 * @param date 日期 
	 * @param d 天数
	 * @return
	 */
	public static Date addDateByDay(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, d);
		return cal.getTime();
	}
	/**
	 * 日期按月计算(负数为减)
	 * @param date 日期 
	 * @param m 月数
	 * @return
	 */
	public static Date addDateByMonth(Date date,int m){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, m);
		return cal.getTime();
	}
	/**
	 * 按年计算
	 * @param date 日期
	 * @param m 年数
	 * @return
	 */
	public static Date addDateByYear(Date date,int m){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, m);
		return cal.getTime();
	}
	/**
	 * 按星期计算
	 * @param date 日期
	 * @param d 星期数
	 * @return
	 */
	public static Date addDateByWeekOfYear(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_YEAR, d);
		return cal.getTime();
	}
	
	/**
	 * 按星期计算
	 * @param date 日期
	 * @param d 星期数
	 * @return
	 */
	public static Date addDateByWeekOfMonth(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_MONTH, d);
		return cal.getTime();
	}
	/**
	 * 日期按小时计算
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date addDateByHour(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, d);
		return cal.getTime();
	}
	/**
	 * 日期按小时计算
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date addDateByHourOfDay(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, d);
		return cal.getTime();
	}
	/**
	 * 日期按分钟计算
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date addDateByMinute(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, d);
		return cal.getTime();
	}
	/**
	 * 日期按秒计算
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date addDateBySecond(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, d);
		return cal.getTime();
	}
	/**
	 * 日期按毫秒计算
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date addDateByMilliSecond(Date date,int d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, d);
		return cal.getTime();
	}
	
}
